home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / src / binutils.252 / gas / atof-gen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-01  |  16.6 KB  |  577 lines

  1. /* atof_generic.c - turn a string of digits into a Flonum
  2.    Copyright (C) 1987, 1990, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4.    This file is part of GAS, the GNU Assembler.
  5.  
  6.    GAS is free software; you can redistribute it and/or modify
  7.    it under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    GAS is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with GAS; see the file COPYING.  If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <ctype.h>
  21. #include <string.h>
  22.  
  23. #include "as.h"
  24.  
  25. #ifndef FALSE
  26. #define FALSE (0)
  27. #endif
  28. #ifndef TRUE
  29. #define TRUE  (1)
  30. #endif
  31.  
  32. /***********************************************************************\
  33.  *                                    *
  34.  *    Given a string of decimal digits , with optional decimal    *
  35.  *    mark and optional decimal exponent (place value) of the        *
  36.  *    lowest_order decimal digit: produce a floating point        *
  37.  *    number. The number is 'generic' floating point: our        *
  38.  *    caller will encode it for a specific machine architecture.    *
  39.  *                                    *
  40.  *    Assumptions                            *
  41.  *        uses base (radix) 2                    *
  42.  *        this machine uses 2's complement binary integers    *
  43.  *        target flonums use "      "         "       "        *
  44.  *        target flonums exponents fit in a long            *
  45.  *                                    *
  46.  \***********************************************************************/
  47.  
  48. /*
  49.  
  50.   Syntax:
  51.  
  52.   <flonum> ::= <optional-sign> <decimal-number> <optional-exponent>
  53.   <optional-sign> ::= '+' | '-' | {empty}
  54.   <decimal-number> ::= <integer>
  55.   | <integer> <radix-character>
  56.   | <integer> <radix-character> <integer>
  57.   | <radix-character> <integer>
  58.  
  59.   <optional-exponent> ::= {empty}
  60.   | <exponent-character> <optional-sign> <integer>
  61.  
  62.   <integer> ::= <digit> | <digit> <integer>
  63.   <digit> ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  64.   <exponent-character> ::= {one character from "string_of_decimal_exponent_marks"}
  65.   <radix-character> ::= {one character from "string_of_decimal_marks"}
  66.  
  67.   */
  68.  
  69. int
  70. atof_generic (address_of_string_pointer,
  71.           string_of_decimal_marks,
  72.           string_of_decimal_exponent_marks,
  73.           address_of_generic_floating_point_number)
  74.      /* return pointer to just AFTER number we read. */
  75.      char **address_of_string_pointer;
  76.      /* At most one per number. */
  77.      const char *string_of_decimal_marks;
  78.      const char *string_of_decimal_exponent_marks;
  79.      FLONUM_TYPE *address_of_generic_floating_point_number;
  80. {
  81.   int return_value;        /* 0 means OK. */
  82.   char *first_digit;
  83.   /* char *last_digit; JF unused */
  84.   int number_of_digits_before_decimal;
  85.   int number_of_digits_after_decimal;
  86.   long decimal_exponent;
  87.   int number_of_digits_available;
  88.   char digits_sign_char;
  89.  
  90.   /*
  91.    * Scan the input string, abstracting (1)digits (2)decimal mark (3) exponent.
  92.    * It would be simpler to modify the string, but we don't; just to be nice
  93.    * to caller.
  94.    * We need to know how many digits we have, so we can allocate space for
  95.    * the digits' value.
  96.    */
  97.  
  98.   char *p;
  99.   char c;
  100.   int seen_significant_digit;
  101.  
  102.   first_digit = *address_of_string_pointer;
  103.   c = *first_digit;
  104.  
  105.   if (c == '-' || c == '+')
  106.     {
  107.       digits_sign_char = c;
  108.       first_digit++;
  109.     }
  110.   else
  111.     digits_sign_char = '+';
  112.  
  113.   switch (first_digit[0])
  114.     {
  115.     case 'n':
  116.     case 'N':
  117.       if (!strncasecmp ("nan", first_digit, 3))
  118.     {
  119.       address_of_generic_floating_point_number->sign = 0;
  120.       address_of_generic_floating_point_number->exponent = 0;
  121.       address_of_generic_floating_point_number->leader =
  122.         address_of_generic_floating_point_number->low;
  123.       *address_of_string_pointer = first_digit + 3;
  124.       return 0;
  125.     }
  126.       break;
  127.  
  128.     case 'i':
  129.     case 'I':
  130.       if (!strncasecmp ("inf", first_digit, 3))
  131.     {
  132.       address_of_generic_floating_point_number->sign =
  133.         digits_sign_char == '+' ? 'P' : 'N';
  134.       address_of_generic_floating_point_number->exponent = 0;
  135.       address_of_generic_floating_point_number->leader =
  136.         address_of_generic_floating_point_number->low;
  137.  
  138.       first_digit += 3;
  139.       if (!strncasecmp ("inity", first_digit, 5))
  140.         first_digit += 5;
  141.  
  142.       *address_of_string_pointer = first_digit;
  143.  
  144.       return 0;
  145.     }
  146.       break;
  147.     }
  148.  
  149.   number_of_digits_before_decimal = 0;
  150.   number_of_digits_after_decimal = 0;
  151.   decimal_exponent = 0;
  152.   seen_significant_digit = 0;
  153.   for (p = first_digit;
  154.        (((c = *p) != '\0')
  155.     && (!c || !strchr (string_of_decimal_marks, c))
  156.     && (!c || !strchr (string_of_decimal_exponent_marks, c)));
  157.        p++)
  158.     {
  159.       if (isdigit (c))
  160.     {
  161.       if (seen_significant_digit || c > '0')
  162.         {
  163.           ++number_of_digits_before_decimal;
  164.           seen_significant_digit = 1;
  165.         }
  166.       else
  167.         {
  168.           first_digit++;
  169.         }
  170.     }
  171.       else
  172.     {
  173.       break;        /* p -> char after pre-decimal digits. */
  174.     }
  175.     }                /* For each digit before decimal mark. */
  176.  
  177. #ifndef OLD_FLOAT_READS
  178.   /* Ignore trailing 0's after the decimal point.  The original code here
  179.    * (ifdef'd out) does not do this, and numbers like
  180.    *    4.29496729600000000000e+09    (2**31)
  181.    * come out inexact for some reason related to length of the digit
  182.    * string.
  183.    */
  184.   if (c && strchr (string_of_decimal_marks, c))
  185.     {
  186.       int zeros = 0;        /* Length of current string of zeros */
  187.  
  188.       for (p++; (c = *p) && isdigit (c); p++)
  189.     {
  190.       if (c == '0')
  191.         {
  192.           zeros++;
  193.         }
  194.       else
  195.         {
  196.           number_of_digits_after_decimal += 1 + zeros;
  197.           zeros = 0;
  198.         }
  199.     }
  200.     }
  201. #else
  202.   if (c && strchr (string_of_decimal_marks, c))
  203.     {
  204.       for (p++;
  205.        (((c = *p) != '\0')
  206.         && (!c || !strchr (string_of_decimal_exponent_marks, c)));
  207.        p++)
  208.     {
  209.       if (isdigit (c))
  210.         {
  211.           /* This may be retracted below. */
  212.           number_of_digits_after_decimal++;
  213.  
  214.           if ( /* seen_significant_digit || */ c > '0')
  215.         {
  216.           seen_significant_digit = TRUE;
  217.         }
  218.         }
  219.       else
  220.         {
  221.           if (!seen_significant_digit)
  222.         {
  223.           number_of_digits_after_decimal = 0;
  224.         }
  225.           break;
  226.         }
  227.     }            /* For each digit after decimal mark. */
  228.     }
  229.  
  230.   while (number_of_digits_after_decimal
  231.      && first_digit[number_of_digits_before_decimal
  232.             + number_of_digits_after_decimal] == '0')
  233.     --number_of_digits_after_decimal;
  234. #endif
  235.  
  236.   if (c && strchr (string_of_decimal_exponent_marks, c))
  237.     {
  238.       char digits_exponent_sign_char;
  239.  
  240.       c = *++p;
  241.       if (c && strchr ("+-", c))
  242.     {
  243.       digits_exponent_sign_char = c;
  244.       c = *++p;
  245.     }
  246.       else
  247.     {
  248.       digits_exponent_sign_char = '+';
  249.     }
  250.  
  251.       for (; (c); c = *++p)
  252.     {
  253.       if (isdigit (c))
  254.         {
  255.           decimal_exponent = decimal_exponent * 10 + c - '0';
  256.           /*
  257.            * BUG! If we overflow here, we lose!
  258.            */
  259.         }
  260.       else
  261.         {
  262.           break;
  263.         }
  264.     }
  265.  
  266.       if (digits_exponent_sign_char == '-')
  267.     {
  268.       decimal_exponent = -decimal_exponent;
  269.     }
  270.     }
  271.  
  272.   *address_of_string_pointer = p;
  273.  
  274.  
  275.  
  276.   number_of_digits_available =
  277.     number_of_digits_before_decimal + number_of_digits_after_decimal;
  278.   return_value = 0;
  279.   if (number_of_digits_available == 0)
  280.     {
  281.       address_of_generic_floating_point_number->exponent = 0;    /* Not strictly necessary */
  282.       address_of_generic_floating_point_number->leader
  283.     = -1 + address_of_generic_floating_point_number->low;
  284.       address_of_generic_floating_point_number->sign = digits_sign_char;
  285.       /* We have just concocted (+/-)0.0E0 */
  286.  
  287.     }
  288.   else
  289.     {
  290.       int count;        /* Number of useful digits left to scan. */
  291.  
  292.       LITTLENUM_TYPE *digits_binary_low;
  293.       unsigned int precision;
  294.       unsigned int maximum_useful_digits;
  295.       unsigned int number_of_digits_to_use;
  296.       unsigned int more_than_enough_bits_for_digits;
  297.       unsigned int more_than_enough_littlenums_for_digits;
  298.       unsigned int size_of_digits_in_littlenums;
  299.       unsigned int size_of_digits_in_chars;
  300.       FLONUM_TYPE power_of_10_flonum;
  301.       FLONUM_TYPE digits_flonum;
  302.  
  303.       precision = (address_of_generic_floating_point_number->high
  304.            - address_of_generic_floating_point_number->low
  305.            + 1);    /* Number of destination littlenums. */
  306.  
  307.       /* Includes guard bits (two littlenums worth) */
  308. #if 0 /* The integer version below is very close, and it doesn't
  309.      require floating point support (which is currently buggy on
  310.      the Alpha).  */
  311.       maximum_useful_digits = (((double) (precision - 2))
  312.                    * ((double) (LITTLENUM_NUMBER_OF_BITS))
  313.                    / (LOG_TO_BASE_2_OF_10))
  314.     + 2;            /* 2 :: guard digits. */
  315. #else
  316.       maximum_useful_digits = (((precision - 2))
  317.                    * ( (LITTLENUM_NUMBER_OF_BITS))
  318.                    * 1000000 / 3321928)
  319.     + 2;            /* 2 :: guard digits. */
  320. #endif
  321.  
  322.       if (number_of_digits_available > maximum_useful_digits)
  323.     {
  324.       number_of_digits_to_use = maximum_useful_digits;
  325.     }
  326.       else
  327.     {
  328.       number_of_digits_to_use = number_of_digits_available;
  329.     }
  330.  
  331.       /* Cast these to SIGNED LONG first, otherwise, on systems with
  332.      LONG wider than INT (such as Alpha OSF/1), unsignedness may
  333.      cause unexpected results.  */
  334.       decimal_exponent += ((long) number_of_digits_before_decimal
  335.                - (long) number_of_digits_to_use);
  336.  
  337. #if 0
  338.       more_than_enough_bits_for_digits
  339.     = ((((double) number_of_digits_to_use) * LOG_TO_BASE_2_OF_10) + 1);
  340. #else
  341.       more_than_enough_bits_for_digits
  342.     = (number_of_digits_to_use * 3321928 / 1000000 + 1);
  343. #endif
  344.  
  345.       more_than_enough_littlenums_for_digits
  346.     = (more_than_enough_bits_for_digits
  347.        / LITTLENUM_NUMBER_OF_BITS)
  348.     + 2;
  349.  
  350.       /* Compute (digits) part. In "12.34E56" this is the "1234" part.
  351.      Arithmetic is exact here. If no digits are supplied then this
  352.      part is a 0 valued binary integer.  Allocate room to build up
  353.      the binary number as littlenums.  We want this memory to
  354.      disappear when we leave this function.  Assume no alignment
  355.      problems => (room for n objects) == n * (room for 1
  356.      object).  */
  357.  
  358.       size_of_digits_in_littlenums = more_than_enough_littlenums_for_digits;
  359.       size_of_digits_in_chars = size_of_digits_in_littlenums
  360.     * sizeof (LITTLENUM_TYPE);
  361.  
  362.       digits_binary_low = (LITTLENUM_TYPE *)
  363.     alloca (size_of_digits_in_chars);
  364.  
  365.       memset ((char *) digits_binary_low, '\0', size_of_digits_in_chars);
  366.  
  367.       /* Digits_binary_low[] is allocated and zeroed. */
  368.  
  369.       /*
  370.        * Parse the decimal digits as if * digits_low was in the units position.
  371.        * Emit a binary number into digits_binary_low[].
  372.        *
  373.        * Use a large-precision version of:
  374.        * (((1st-digit) * 10 + 2nd-digit) * 10 + 3rd-digit ...) * 10 + last-digit
  375.        */
  376.  
  377.       for (p = first_digit, count = number_of_digits_to_use; count; p++, --count)
  378.     {
  379.       c = *p;
  380.       if (isdigit (c))
  381.         {
  382.           /*
  383.            * Multiply by 10. Assume can never overflow.
  384.            * Add this digit to digits_binary_low[].
  385.            */
  386.  
  387.           long carry;
  388.           LITTLENUM_TYPE *littlenum_pointer;
  389.           LITTLENUM_TYPE *littlenum_limit;
  390.  
  391.           littlenum_limit = digits_binary_low
  392.         + more_than_enough_littlenums_for_digits
  393.         - 1;
  394.  
  395.           carry = c - '0';    /* char -> binary */
  396.  
  397.           for (littlenum_pointer = digits_binary_low;
  398.            littlenum_pointer <= littlenum_limit;
  399.            littlenum_pointer++)
  400.         {
  401.           long work;
  402.  
  403.           work = carry + 10 * (long) (*littlenum_pointer);
  404.           *littlenum_pointer = work & LITTLENUM_MASK;
  405.           carry = work >> LITTLENUM_NUMBER_OF_BITS;
  406.         }
  407.  
  408.           if (carry != 0)
  409.         {
  410.           /*
  411.            * We have a GROSS internal error.
  412.            * This should never happen.
  413.            */
  414.           as_fatal ("failed sanity check.");
  415.         }
  416.         }
  417.       else
  418.         {
  419.           ++count;        /* '.' doesn't alter digits used count. */
  420.         }
  421.     }
  422.  
  423.  
  424.       /*
  425.        * Digits_binary_low[] properly encodes the value of the digits.
  426.        * Forget about any high-order littlenums that are 0.
  427.        */
  428.       while (digits_binary_low[size_of_digits_in_littlenums - 1] == 0
  429.          && size_of_digits_in_littlenums >= 2)
  430.     size_of_digits_in_littlenums--;
  431.  
  432.       digits_flonum.low = digits_binary_low;
  433.       digits_flonum.high = digits_binary_low + size_of_digits_in_littlenums - 1;
  434.       digits_flonum.leader = digits_flonum.high;
  435.       digits_flonum.exponent = 0;
  436.       /*
  437.        * The value of digits_flonum . sign should not be important.
  438.        * We have already decided the output's sign.
  439.        * We trust that the sign won't influence the other parts of the number!
  440.        * So we give it a value for these reasons:
  441.        * (1) courtesy to humans reading/debugging
  442.        *     these numbers so they don't get excited about strange values
  443.        * (2) in future there may be more meaning attached to sign,
  444.        *     and what was
  445.        *     harmless noise may become disruptive, ill-conditioned (or worse)
  446.        *     input.
  447.        */
  448.       digits_flonum.sign = '+';
  449.  
  450.       {
  451.     /*
  452.      * Compute the mantssa (& exponent) of the power of 10.
  453.      * If sucessful, then multiply the power of 10 by the digits
  454.      * giving return_binary_mantissa and return_binary_exponent.
  455.      */
  456.  
  457.     LITTLENUM_TYPE *power_binary_low;
  458.     int decimal_exponent_is_negative;
  459.     /* This refers to the "-56" in "12.34E-56". */
  460.     /* FALSE: decimal_exponent is positive (or 0) */
  461.     /* TRUE:  decimal_exponent is negative */
  462.     FLONUM_TYPE temporary_flonum;
  463.     LITTLENUM_TYPE *temporary_binary_low;
  464.     unsigned int size_of_power_in_littlenums;
  465.     unsigned int size_of_power_in_chars;
  466.  
  467.     size_of_power_in_littlenums = precision;
  468.     /* Precision has a built-in fudge factor so we get a few guard bits. */
  469.  
  470.     decimal_exponent_is_negative = decimal_exponent < 0;
  471.     if (decimal_exponent_is_negative)
  472.       {
  473.         decimal_exponent = -decimal_exponent;
  474.       }
  475.  
  476.     /* From now on: the decimal exponent is > 0. Its sign is seperate. */
  477.  
  478.     size_of_power_in_chars = size_of_power_in_littlenums
  479.       * sizeof (LITTLENUM_TYPE) + 2;
  480.  
  481.     power_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars);
  482.     temporary_binary_low = (LITTLENUM_TYPE *) alloca (size_of_power_in_chars);
  483.     memset ((char *) power_binary_low, '\0', size_of_power_in_chars);
  484.     *power_binary_low = 1;
  485.     power_of_10_flonum.exponent = 0;
  486.     power_of_10_flonum.low = power_binary_low;
  487.     power_of_10_flonum.leader = power_binary_low;
  488.     power_of_10_flonum.high = power_binary_low + size_of_power_in_littlenums - 1;
  489.     power_of_10_flonum.sign = '+';
  490.     temporary_flonum.low = temporary_binary_low;
  491.     temporary_flonum.high = temporary_binary_low + size_of_power_in_littlenums - 1;
  492.     /*
  493.      * (power) == 1.
  494.      * Space for temporary_flonum allocated.
  495.      */
  496.  
  497.     /*
  498.      * ...
  499.      *
  500.      * WHILE    more bits
  501.      * DO    find next bit (with place value)
  502.      *    multiply into power mantissa
  503.      * OD
  504.      */
  505.     {
  506.       int place_number_limit;
  507.       /* Any 10^(2^n) whose "n" exceeds this */
  508.       /* value will fall off the end of */
  509.       /* flonum_XXXX_powers_of_ten[]. */
  510.       int place_number;
  511.       const FLONUM_TYPE *multiplicand;    /* -> 10^(2^n) */
  512.  
  513.       place_number_limit = table_size_of_flonum_powers_of_ten;
  514.  
  515.       multiplicand = (decimal_exponent_is_negative
  516.               ? flonum_negative_powers_of_ten
  517.               : flonum_positive_powers_of_ten);
  518.  
  519.       for (place_number = 1;/* Place value of this bit of exponent. */
  520.            decimal_exponent;/* Quit when no more 1 bits in exponent. */
  521.            decimal_exponent >>= 1, place_number++)
  522.         {
  523.           if (decimal_exponent & 1)
  524.         {
  525.           if (place_number > place_number_limit)
  526.             {
  527.               /* The decimal exponent has a magnitude so great
  528.                  that our tables can't help us fragment it.
  529.                  Although this routine is in error because it
  530.                  can't imagine a number that big, signal an
  531.                  error as if it is the user's fault for
  532.                  presenting such a big number.  */
  533.               return_value = ERROR_EXPONENT_OVERFLOW;
  534.               /* quit out of loop gracefully */
  535.               decimal_exponent = 0;
  536.             }
  537.           else
  538.             {
  539. #ifdef TRACE
  540.               printf ("before multiply, place_number = %d., power_of_10_flonum:\n",
  541.                   place_number);
  542.  
  543.               flonum_print (&power_of_10_flonum);
  544.               (void) putchar ('\n');
  545. #endif
  546.               flonum_multip (multiplicand + place_number,
  547.                      &power_of_10_flonum, &temporary_flonum);
  548.               flonum_copy (&temporary_flonum, &power_of_10_flonum);
  549.             } /* If this bit of decimal_exponent was computable.*/
  550.         } /* If this bit of decimal_exponent was set. */
  551.         } /* For each bit of binary representation of exponent */
  552. #ifdef TRACE
  553.       printf (" after computing power_of_10_flonum: ");
  554.       flonum_print (&power_of_10_flonum);
  555.       (void) putchar ('\n');
  556. #endif
  557.     }
  558.  
  559.       }
  560.  
  561.       /*
  562.        * power_of_10_flonum is power of ten in binary (mantissa) , (exponent).
  563.        * It may be the number 1, in which case we don't NEED to multiply.
  564.        *
  565.        * Multiply (decimal digits) by power_of_10_flonum.
  566.        */
  567.  
  568.       flonum_multip (&power_of_10_flonum, &digits_flonum, address_of_generic_floating_point_number);
  569.       /* Assert sign of the number we made is '+'. */
  570.       address_of_generic_floating_point_number->sign = digits_sign_char;
  571.  
  572.     }
  573.   return return_value;
  574. }
  575.  
  576. /* end of atof_generic.c */
  577.